package com.chuangke.metadata.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chuangke.common.db.page.CkPageQuery;
import com.chuangke.common.http.HttpResult;
import com.chuangke.metadata.entity.SysGridColumn;
import com.chuangke.metadata.entity.SysGridModel;
import com.chuangke.metadata.service.SysGridModelService;

/**
 * @author chuangke
 * @date 2022-10-20
 */
@RestController
@RequestMapping("grid/model")
public class SysGridModelController {

	@Autowired
	private SysGridModelService sysGridModelService;

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/page")
	public HttpResult page(@RequestBody Map<String, Object> pageQuery) {
		return HttpResult.ok(sysGridModelService.page(new CkPageQuery(pageQuery)));
	}
	
	/**
	 * 只保存模型表头
	 * @param record
	 * @return
	 */
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/savemodel")
	public HttpResult saveModel(@RequestBody SysGridModel record) {
		sysGridModelService.saveSysGridMode(record);
		return HttpResult.ok();
	}
	
	/**
	 * 只保存模型列信息
	 * @param record
	 * @return
	 */
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/savecolumn")
	public HttpResult saveColum(@RequestBody SysGridModel record) {
		sysGridModelService.saveSysGridModelColumn(record) ;
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/save")
	public HttpResult save(@RequestBody SysGridModel record) {
		sysGridModelService.saveOrUpdate(record);
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/delete")
	public HttpResult delete(@RequestBody List<String> ids) {
		sysGridModelService.delete(ids);
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/{id}")
	public HttpResult findLevelDept(@PathVariable("id") String id) {
		return HttpResult.ok(sysGridModelService.load(id));
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/deleteColumns")
	public HttpResult deleteColumns(@RequestBody List<String> ids) {
		sysGridModelService.delModelColumns(ids);
		return HttpResult.ok();
	}


	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/addModelCoumn")
	public HttpResult addModelCoumn(@RequestBody SysGridColumn sysGridColumn) {
		sysGridModelService.addModelCoumn(sysGridColumn);
		return HttpResult.ok();
	}
}
